<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller {

    public function cart()
    {
        $cart = D('cart');
        $data = I('get.');
        $goods = $cart->where('uid='.$data['uid'])->field('id,good_id,number')->group('good_id')->select();
        foreach ($goods as $key => $value) {
            $ids[] = $value['good_id'];
        }
        
        if(empty($data['good_id'])){
            foreach ($goods as $key => $value) {
                $good = D('Goods')->find($value['good_id']);
                $info[$key]['id'] =$good['id'];
                $info[$key]['product_name'] = $good['product_name'];
                $info[$key]['good_sn'] = $good['good_sn'];
                $info[$key]['market_price'] = $good['market_price'];
                $info[$key]['shop_price'] = $good['shop_price'];
                $info[$key]['img'] = $good['img1'];
                $info[$key]['good_number'] = $good['goods_number'];
                $info[$key]['cart_id'] = $goods[$key]['id'];
                $info[$key]['number'] = $goods[$key]['number']; 
            }
        }else{
            if(in_array($data['good_id'], $ids)){
                $where['uid'] = $data['uid'];
                $where['good_id'] = $data['good_id'];
                $z = $cart->where($where)->setInc('number',$data['number']);
            }else{
                $data['id'] = $cart->max('id')+1;
                $z = $cart->add($data); 
            }
            
        }
        
        kuayu();
        echo json_encode($info);

    }
    public function delete_cart()
    {
        $data = I('get.');
        $where = array();
        $where['uid'] = $data['uid'];
        $where['good_id'] = array('in',$data['good_id']);
        $z = M('Cart')->where($where)->delete();
        if($z){
            $d['code'] = 1;
            $d['msh'] = '清除成功';
        }else{
            $d['code'] = 2;
            $d['msg'] = '清除失败';
        }
        kuayu();
        echo json_encode($d);
    }
    public function change_cart()
    {
        $data = I('get.');
        $where['id'] = $data['cart_id'];
        $where['uid'] = $data['user_id'];
        $number = $data['number'];
        $z = D('Cart')->where($where)->setField('number',$number);

    }

    public function address(){
        $data = I('get.');
        $address = D('Users_address');
        $id = $address->max('id');
        $data['id'] = $id+1;
        $z = $address->add($data);
        if($z){
            $a = D('Users')->where('id='.$data['user_id'])->setField('address_id',$data['id']);
        }else{
            echo 'false';
        }


        // else{
        //     $uid = I('get.uid');
        //     $address_data = M('Users_adress')->where('user_id='.$uid)->select();
        //     kuayu();
        //     echo json_encode($address_data); 
        // }

    }
    public function check()
    {
//        $address_id = D('Users')->where('id='.$data['uid'])->getfield('address_id');
//        $address = D('Users_address');
//        $info['default_address'] = $address->find($address_id);
//        $info['address'] = $address->where('user_id='.$data['uid'])->select();
        $data = I('get.');
        $info['total_credit'] = D('Users')->where('id='.$data['uid'])->getfield('total_credit');
        $info['goods'] = quert_cart_good($data['cart_id'],$data['uid']);
        kuayu();
        echo json_encode($info);
    }
    public function create_order()
    {   
        $data = I('get.');
        $cart_info = query_address($data['uid']);
        $cart_good = quert_cart_good($data['cart_id'],$data['uid']);

        
        $order_info = D('Order_info');
        $id = $order_info->max('id');
        $info['id'] = $id+1; 
        $info['order_sn'] = "WLGO".str_pad($id+1,11,"0",STR_PAD_LEFT);
        $info['user_id'] = $cart_info['user_id'];
        $info['order_status'] = 0;
        $info['consignee'] = $cart_info['consignee'];
        $info['address'] = $cart_info['address'];
        $info['detail_address'] = $cart_info['detail_address'];
        $info['zipcode'] = $cart_info['zipcode'];
        $info['tel'] = $cart_info['tel'];
        $info['order_time'] = time();
        $total = 0;
        foreach ($cart_good as $k=>$v) {
            $total += floatval($v['good_number']*$v['shop_price']);
        }
        $info['total_price'] = $total;



        $z = $order_info->add($info);
        if($z){
            foreach ($cart_good as $key => $value) {
                $id = M('Order_good')->max('id');
                $good['id'] = $id+1;
                $good['order_id'] = $info['id'];
                $good['good_id'] = $value['good_id'];
                $good['good_name'] = $value['good_name'];
                $good['good_sn'] = $value['good_sn'];
                $good['good_number'] = $value['good_number'];
                $good['market_price'] = $value['market_price'];
                $good['shop_price'] = $value['shop_price'];
            }
        }

        var_dump($data);die;
    }
}